<template>
  <div>
    <el-card>
      <!--搜索框11-->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-input placeholder="请输入房间类型" clearable v-model="name" @clear="search">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="openAddRoomTypeApplication">创建房间类型</el-button>
        </el-col>
      </el-row>
      <!--添加房间类型-->
      <el-dialog title="添加房间类型" :visible.sync="addRoomTypeFormVisible" @close="closeAddRoomTypeForm"
                 :close-on-click-modal="false">
        <el-form ref="addRoomTypeForm" :rules="addRoomTypeFormRules" :model="addRoomTypeForm" label-width="150px"
                 size="big">
          <el-form-item label="房间类型" prop="housetype">
            <el-input v-model="addRoomTypeForm.housetype" placeholder="请输入房间类型" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="床位月价格" prop="price">
            <el-input v-model="addRoomTypeForm.price" placeholder="请输入床位月价格" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="desc">
            <el-input type="textarea" v-model="addRoomTypeForm.desc" placeholder="请输入描述"
                      style="width: 220px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAddRoomTypeForm">取 消</el-button>
          <el-button type="primary" @click="AddRoomTypeFormOK">确 定</el-button>
        </div>
      </el-dialog>
      <!--主体数据-->
      <template>
        <el-table
            :data="RoomTypeApplicationList"
            style="width: 100%"
            :row-class-name="tableRowClassName"

        >
          <el-table-column
              label="编号"
              type="index"
              width="200">
          </el-table-column>
          <el-table-column
              prop="housetype"
              label="房间类型"
              width="250">
          </el-table-column>
          <el-table-column
              prop="desc"
              label="房间描述"
              width="350">
          </el-table-column>
          <el-table-column
              prop="price"
              label="价格"
              width="250">
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  @click="handleEdit(scope.row.id)">编辑
              </el-button>
              <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!--分页-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 6 , 12, 24]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
      <!--编辑身高-->
      <el-dialog title="编辑房间类型" :visible.sync="modifyRoomTypeFormVisible" @close="closeModifyRoomTypeForm"
                 :close-on-click-modal="false">
        <el-form ref="modifyRoomTypeForm" :rules="modifyRoomTypeFormRules" :model="modifyRoomTypeForm"
                 label-width="150px" size="big">
          <el-form-item label="房间类型" prop="housetype">
            <el-input v-model="modifyRoomTypeForm.housetype" placeholder="请输入房间类型" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="床位月价格/元" prop="price">
            <el-input v-model="modifyRoomTypeForm.price" placeholder="请输入床位月价格" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="desc">
            <el-input type="textarea" v-model="modifyRoomTypeForm.desc" placeholder="请输入描述"
                      style="width: 220px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeModifyRoomTypeForm">取 消</el-button>
          <el-button type="primary" @click="modifyRoomTypeFormOK">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {toRefs} from "vue";

export default {
  data() {
    return {
      addRoomTypeForm: {
        housetype: 0,
        price: '',
        desc: '',
      },
      modifyRoomTypeForm: {
        housetype: 0,
        price: '',
        desc: '',
      },
      RoomTypeApplicationList: [],
      modifyRoomTypeFormVisible: false,
      addRoomTypeFormVisible: false,
      name: '',
      pageNum: 1,
      pageSize: 3,
      total: 0,
      addRoomTypeFormRules: {
        housetype: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        price: [
          {required: true, message: '不能为空', trigger: 'blur'},
          {pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/, message: '必须为大于0的数字', trigger: 'blur'}
        ],
      },
      modifyRoomTypeFormRules: {
        housetype: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        price: [
          {required: true, message: '不能为空', trigger: 'blur'},
          {pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/, message: '必须为大于0的数字', trigger: 'blur'}
        ],
      },
    };
  },
  methods: {
    tableRowClassName({rowIndex}) {
      if (rowIndex === 0) {
        return 'white-row'; // 第一行为白色
      } else if (rowIndex % 4 === 1) {
        return 'warning-row'; // 第二行和每隔三行后的行为绿色
      } else if (rowIndex % 2 === 0) {
        return ''; // 第二行和每隔三行后的行为绿色
      } else if (rowIndex % 3 === 2) {
        return 'white-row'; // 第三行和每隔三行后的行为白色
      } else {
        return 'success-row'; // 每隔三行后的行为黄色
      }
    },

    handleDelete(id) {
      this.$confirm("你确定要删除此记录吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let url = '/housetype/delete/' + id;
        this.$axios.delete(url).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              message: res.data.msg,
              type: 'success',
              duration: 1000
            });
            this.initRoomTypeApplicationList();
          } else {
            this.$message({
              message: res.data.msg,
              type: 'error',
              duration: 1000
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "取消成功"
        })
      })
    },
    openAddRoomTypeApplication(id) {
      this.addRoomTypeFormVisible = true;
      this.addRoomTypeForm = {};
    },
    closeAddRoomTypeForm() {
      this.addRoomTypeFormVisible = false;
    },
    //修改申请表
    handleEdit(id) {
      this.modifyRoomTypeFormVisible = true;
      let url = "/housetype/one/" + id;
      this.$axios.get(url).then((res) => {
        if (res.data.code == 200) {
          this.modifyRoomTypeForm = res.data.data;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //确认添加
    AddRoomTypeFormOK() {
      this.$refs.addRoomTypeForm.validate((vaild) => {
        if (!vaild) {
          return
        } else {
          this.$confirm("你确定要创建吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            let url = '/housetype/add'
            this.$axios.post(url, this.addRoomTypeForm).then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  message: res.data.msg,
                  type: 'success',
                  duration: 1000
                });
                this.initRoomTypeApplicationList();
                this.addRoomTypeFormVisible = false;
              } else {
                this.$message({
                  message: res.data.msg,
                  type: 'error',
                  duration: 1000
                });
              }
            })
          }).catch(() => {
            this.$message({
              type: "info",
              message: "取消成功"
            })
          })
        }
      })
    },
    //确认修改
    modifyRoomTypeFormOK() {
      this.$refs.modifyRoomTypeForm.validate((vaild) => {
        if (!vaild) {
          return
        } else {
          let url = '/housetype/modify'
          this.$axios.put(url, this.modifyRoomTypeForm).then((res) => {
            if (res.data.code == 200) {
              this.$message({
                message: res.data.msg,
                type: 'success',
                duration: 1000
              });
              this.initRoomTypeApplicationList();
              this.modifyRoomTypeFormVisible = false;
            } else {
              this.$message({
                message: res.data.msg,
                type: 'error',
                duration: 1000
              });
            }
          })
        }
      })
    },
    //关闭修改对话框
    closeModifyRoomTypeForm() {
      this.modifyRoomTypeFormVisible = false;
    },
    //查询长者
    search() {
      this.pageNum = 1;
      this.initRoomTypeApplicationList();
    },
    //初始化数据
    initRoomTypeApplicationList() {
      let url = "/housetype/list"
      this.$axios.get(url, {
        params: {
          name: this.name,
          now: this.pageNum,
          size: this.pageSize
        }
      }).then((res) => {
        if (res.data.code == 200) {
          this.RoomTypeApplicationList = res.data.data.list;
          this.pageNum = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
          this.total = res.data.data.total;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //改变条数查询
    handleSizeChange(size) {
      this.pageSize = size;
      this.pageNum = 1;
      this.initRoomTypeApplicationList();
    },
    //改变页数查询
    handleCurrentChange(now) {
      this.pageNum = now;
      this.initRoomTypeApplicationList();
    },
  },
  created() {
    this.initRoomTypeApplicationList();
  }
}
</script>

<style>
/*.el-table .warning-row {*/
/*  background: #cd8703;*/
/*}*/

/*.el-table .success-row {*/
/*  background: #f0f9eb;*/
/*}*/

</style>